package com.ssmp.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssmp.domain.Book;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author newThread
 * @className BookDaoTestCase
 * @date 2022/2/4 17:20
 */
@SpringBootTest
public class BookDaoTestCase {
    @Autowired
    private BookDao bookDao;

    @Test
    void testGetById() {
        System.out.println(bookDao.getById(1));
    }
//    增
    @Test
    void addBook(){
        Book book = new Book();
       book.setName("springboot");
       book.setType("后端开发框架");
       book.setDescription("SpringBoot技术由Pivotal团队研发制作，功能的话简单概括就是加速Spring程序的开发，这个加速要从如下两个方面来说\n" +
               "\n" +
               "- Spring程序初始搭建过程\n" +
               "- Spring程序的开发过程");
       bookDao.insert(book);
    }

//    删
    @Test
void delBook(){
        bookDao.deleteById(13);
}
//    改
    @Test
    void updateBook(){
        Book book = new Book();
        book.setId(10);
        book.setName("spring");
        book.setType("后端开发");
        book.setDescription("Spring技术");
        bookDao.updateById(book);
    }
//查
    @Test
    void queryBook(){
        bookDao.selectList(null);
    }
//    分页
    @Test
    void pages(){
        IPage<Book> page = new Page<>(2,5);
        //IPage page =new Page(1,5);
        bookDao.selectPage(page,null);
        System.out.println("当前页码值："+page.getCurrent());
        System.out.println("每页显示数："+page.getSize());
        System.out.println("数据总量："+page.getTotal());
        System.out.println("总页数："+page.getPages());
        System.out.println("详细数据："+page.getRecords());

    }
//    条件查询
    @Test
    void QueryWrapperTest(){
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Book> result = bookQueryWrapper.like("name","spring");
        System.out.println(result);
        bookDao.selectList(result);
    }
    @Test
    void LambdaQueryWrapperTest1(){
        LambdaQueryWrapper<Book> bookQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Book> result = bookQueryWrapper.like(Book::getName,"spring");
        System.out.println(result);
        bookDao.selectList(result);
    }
    @Test
    void LambdaQueryWrapperTest2(){
        String queryString = "spring";
        LambdaQueryWrapper<Book> bookQueryWrapper = new LambdaQueryWrapper<>();
       /* if(queryString!=null){
           bookQueryWrapper.like(Book::getName,queryString);
        }*/
      //  bookQueryWrapper.like(queryString!=null,Book::getName,queryString);
        bookQueryWrapper.like(Strings.isNotEmpty(queryString),Book::getName,queryString);

        bookDao.selectList(bookQueryWrapper);
    }
}
